﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class AccessorialDA : IAccessorialDA
    {
        private string ACCESSORIAL_ID = "@ACCESSORIAL_ID";
        private string BILLING_SERVICE_ID = "@BILLING_SERVICE_ID";
        private string CHARGE_INVOCATION = "@CHARGE_INVOCATION";
        private string CHARGE_PER_UNIT = "@CHARGE_PER_UNIT";
        private string CHARGE_UNIT = "@CHARGE_UNIT";
        private string COST_INVOCATION = "@COST_INVOCATION";
        private string COST_PER_UNIT = "@COST_PER_UNIT";
        private string COST_RATE_ID = "@COST_RATE_ID";
        private string COST_TYPE = "@COST_TYPE";
        private string COST_UNIT = "@COST_UNIT";
        private string CREATED_BY = "@CREATED_BY";
        private string DESCR = "@DESCR";
        private string GENERAL_LEDGER_ID = "@GENERAL_LEDGER_ID";
        private string IS_ACTIVE = "@IS_ACTIVE";
        private string OWNER_ID = "@OWNER_ID";
        private string RATE = "@RATE";
        private string RATE_ID = "@RATE_ID";
        private string SQL_CHECK_ACCESSORIAL_ID_UNIQUE = " SELECT COUNT(1) FROM @_@ACCESSORIAL WHERE ACCESSORIAL_ID = @ACCESSORIAL_ID ";
        private string SQL_DELETE_ACCESSORIAL = " DELETE FROM @_@ACCESSORIAL WHERE ACCESSORIAL_ID = @ACCESSORIAL_ID ";
        private string SQL_INSERT_ACCESSORIAL = " INSERT INTO @_@ACCESSORIAL ( WH_ID, ACCESSORIAL_ID, BILLING_SERVICE_ID, OWNER_ID, DESCR, TAX_GROUP_ID, GENERAL_LEDGER_ID, RATE_ID, CHARGE_UNIT, CHARGE_PER_UNIT, CHARGE_INVOCATION, COST_RATE_ID, COST_UNIT, COST_PER_UNIT, COST_INVOCATION, IS_ACTIVE, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE,COST_TYPE,RATE) VALUES ( @WH_ID, @ACCESSORIAL_ID, @BILLING_SERVICE_ID, @OWNER_ID, @DESCR, @TAX_GROUP_ID, @GENERAL_LEDGER_ID, @RATE_ID, @CHARGE_UNIT, @CHARGE_PER_UNIT, @CHARGE_INVOCATION, @COST_RATE_ID, @COST_UNIT, @COST_PER_UNIT, @COST_INVOCATION, @IS_ACTIVE, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE(),@COST_TYPE,@RATE )  ";
        private string SQL_SELECT_ALL_ACCESSORIAL = " SELECT  ROWID, WH_ID, ACCESSORIAL_ID, BILLING_SERVICE_ID, OWNER_ID, DESCR, TAX_GROUP_ID, GENERAL_LEDGER_ID, RATE_ID, CHARGE_UNIT, CHARGE_PER_UNIT, CHARGE_INVOCATION, COST_RATE_ID, COST_UNIT, COST_PER_UNIT, COST_INVOCATION, IS_ACTIVE, TS, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE,COST_TYPE,RATE  FROM  @_@ACCESSORIAL ";
        private string SQL_UPDATE_ACCESSORIAL = " UPDATE @_@ACCESSORIAL SET WH_ID = @WH_ID, BILLING_SERVICE_ID = @BILLING_SERVICE_ID, OWNER_ID = @OWNER_ID, DESCR = @DESCR, TAX_GROUP_ID = @TAX_GROUP_ID, GENERAL_LEDGER_ID = @GENERAL_LEDGER_ID, RATE_ID = @RATE_ID, CHARGE_UNIT = @CHARGE_UNIT, CHARGE_PER_UNIT = @CHARGE_PER_UNIT, CHARGE_INVOCATION = @CHARGE_INVOCATION, COST_RATE_ID = @COST_RATE_ID, COST_UNIT = @COST_UNIT, COST_PER_UNIT = @COST_PER_UNIT, COST_INVOCATION = @COST_INVOCATION, IS_ACTIVE = @IS_ACTIVE, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE(),COST_TYPE = @COST_TYPE,RATE = @RATE WHERE ACCESSORIAL_ID = @ACCESSORIAL_ID  ";
        private string TAX_GROUP_ID = "@TAX_GROUP_ID";
        private string TS = "@TS";
        private string UPDATED_BY = "@UPDATED_BY";
        private string WH_ID = "@WH_ID";

        public bool CheckAccessorialIDUnique(string accessorialID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ACCESSORIAL_ID", accessorialID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_ACCESSORIAL_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckAccessorialIDUnique(DataBase dataBase, DbTransaction tran, string accessorialID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ACCESSORIAL_ID", accessorialID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_ACCESSORIAL_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteAccessorial(List<string> accessorialID, string whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int j = 0; j < accessorialID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ACCESSORIAL_ID", accessorialID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_ACCESSORIAL.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteAccessorial(DataBase dataBase, DbTransaction tran, List<string> accessorialID, string whLoginID)
        {
            int result = 0;
            try
            {
                for (int j = 0; j < accessorialID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ACCESSORIAL_ID", accessorialID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_ACCESSORIAL.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public AccessorialInfo GetAccessorialByID(string accessorialID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_ACCESSORIAL.Replace("@_@", whLoginID) + " WHERE ACCESSORIAL_ID = @ACCESSORIAL_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ACCESSORIAL_ID", accessorialID) };
            AccessorialInfo accessorialInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    accessorialInfo = new AccessorialInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["ACCESSORIAL_ID"].ToString(), reader["BILLING_SERVICE_ID"].ToString(), reader["OWNER_ID"].ToString(), reader["DESCR"].ToString(), reader["TAX_GROUP_ID"].ToString(), reader["GENERAL_LEDGER_ID"].ToString(), reader["RATE_ID"].ToString(), reader["CHARGE_UNIT"].ToString(), (reader["CHARGE_PER_UNIT"].ToString() != "") ? decimal.Parse(reader["CHARGE_PER_UNIT"].ToString()) : 0M, reader["CHARGE_INVOCATION"].ToString(), reader["COST_RATE_ID"].ToString(), reader["COST_UNIT"].ToString(), (reader["COST_PER_UNIT"].ToString() != "") ? decimal.Parse(reader["COST_PER_UNIT"].ToString()) : 0M, reader["COST_INVOCATION"].ToString(), reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime(), reader["COST_TYPE"].ToString(), (reader["RATE"].ToString() != "") ? decimal.Parse(reader["RATE"].ToString()) : 0M);
                }
            }
            return accessorialInfo;
        }

        public AccessorialInfo GetAccessorialByID(DataBase dataBase, DbTransaction tran, string accessorialID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_ACCESSORIAL.Replace("@_@", whLoginID) + " WHERE ACCESSORIAL_ID = @ACCESSORIAL_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ACCESSORIAL_ID", accessorialID) };
            AccessorialInfo accessorialInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                accessorialInfo = new AccessorialInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["ACCESSORIAL_ID"].ToString(), reader["BILLING_SERVICE_ID"].ToString(), reader["OWNER_ID"].ToString(), reader["DESCR"].ToString(), reader["TAX_GROUP_ID"].ToString(), reader["GENERAL_LEDGER_ID"].ToString(), reader["RATE_ID"].ToString(), reader["CHARGE_UNIT"].ToString(), (reader["CHARGE_PER_UNIT"].ToString() != "") ? decimal.Parse(reader["CHARGE_PER_UNIT"].ToString()) : 0M, reader["CHARGE_INVOCATION"].ToString(), reader["COST_RATE_ID"].ToString(), reader["COST_UNIT"].ToString(), (reader["COST_PER_UNIT"].ToString() != "") ? decimal.Parse(reader["COST_PER_UNIT"].ToString()) : 0M, reader["COST_INVOCATION"].ToString(), reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime(), reader["COST_TYPE"].ToString(), (reader["RATE"].ToString() != "") ? decimal.Parse(reader["RATE"].ToString()) : 0M);
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return accessorialInfo;
        }

        public DataSet GetAccessorialByQueryList(List<string> sqlWhere, AccessorialQueryEntity accessorialQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_ACCESSORIAL.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!accessorialQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, accessorialQuery.CurrentPage, accessorialQuery.PageSize, accessorialQuery.SortField, accessorialQuery.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetAccessorialByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, AccessorialQueryEntity accessorialQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_ACCESSORIAL.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            temp = PagingHelper.GetPagingSQL(temp, accessorialQuery.CurrentPage, accessorialQuery.PageSize, accessorialQuery.SortField, accessorialQuery.SortDirection);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public DataSet GetAllAccessorial(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_ACCESSORIAL.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllAccessorial(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_ACCESSORIAL.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public int InsertAccessorial(AccessorialInfo accessorialInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                accessorialInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_Accessorial_Parameters(accessorialInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_ACCESSORIAL.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int InsertAccessorial(DataBase dataBase, DbTransaction tran, AccessorialInfo accessorialInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                accessorialInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_Accessorial_Parameters(accessorialInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_ACCESSORIAL.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_Accessorial_Parameters(AccessorialInfo accessorialInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { 
                new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.ACCESSORIAL_ID, SqlDbType.VarChar, 20), new SqlParameter(this.BILLING_SERVICE_ID, SqlDbType.VarChar, 20), new SqlParameter(this.OWNER_ID, SqlDbType.VarChar, 20), new SqlParameter(this.DESCR, SqlDbType.VarChar, 100), new SqlParameter(this.TAX_GROUP_ID, SqlDbType.VarChar, 20), new SqlParameter(this.GENERAL_LEDGER_ID, SqlDbType.VarChar, 20), new SqlParameter(this.RATE_ID, SqlDbType.VarChar, 20), new SqlParameter(this.CHARGE_UNIT, SqlDbType.VarChar, 20), new SqlParameter(this.CHARGE_PER_UNIT, SqlDbType.Decimal, 13), new SqlParameter(this.CHARGE_INVOCATION, SqlDbType.VarChar, 20), new SqlParameter(this.COST_RATE_ID, SqlDbType.VarChar, 20), new SqlParameter(this.COST_UNIT, SqlDbType.VarChar, 20), new SqlParameter(this.COST_PER_UNIT, SqlDbType.Decimal, 13), new SqlParameter(this.COST_INVOCATION, SqlDbType.VarChar, 20), new SqlParameter(this.IS_ACTIVE, SqlDbType.VarChar, 1), 
                new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.COST_TYPE, SqlDbType.VarChar, 50), new SqlParameter(this.RATE, SqlDbType.Decimal, 13)
             };
            if (!string.IsNullOrEmpty(accessorialInfo.WhID))
            {
                paramArray[0].Value = accessorialInfo.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = accessorialInfo.AccessorialID;
            if (!string.IsNullOrEmpty(accessorialInfo.BillingServiceID))
            {
                paramArray[2].Value = accessorialInfo.BillingServiceID;
            }
            else
            {
                paramArray[2].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(accessorialInfo.OwnerID))
            {
                paramArray[3].Value = accessorialInfo.OwnerID;
            }
            else
            {
                paramArray[3].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(accessorialInfo.Descr))
            {
                paramArray[4].Value = accessorialInfo.Descr;
            }
            else
            {
                paramArray[4].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(accessorialInfo.TaxGroupID))
            {
                paramArray[5].Value = accessorialInfo.TaxGroupID;
            }
            else
            {
                paramArray[5].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(accessorialInfo.GeneralLedgerID))
            {
                paramArray[6].Value = accessorialInfo.GeneralLedgerID;
            }
            else
            {
                paramArray[6].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(accessorialInfo.RateID))
            {
                paramArray[7].Value = accessorialInfo.RateID;
            }
            else
            {
                paramArray[7].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(accessorialInfo.ChargeUnit))
            {
                paramArray[8].Value = accessorialInfo.ChargeUnit;
            }
            else
            {
                paramArray[8].Value = DBNull.Value;
            }
            paramArray[9].Value = accessorialInfo.ChargePerUnit;
            if (!string.IsNullOrEmpty(accessorialInfo.ChargeInvocation))
            {
                paramArray[10].Value = accessorialInfo.ChargeInvocation;
            }
            else
            {
                paramArray[10].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(accessorialInfo.CostRateID))
            {
                paramArray[11].Value = accessorialInfo.CostRateID;
            }
            else
            {
                paramArray[11].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(accessorialInfo.CostUnit))
            {
                paramArray[12].Value = accessorialInfo.CostUnit;
            }
            else
            {
                paramArray[12].Value = DBNull.Value;
            }
            paramArray[13].Value = accessorialInfo.CostPerUnit;
            if (!string.IsNullOrEmpty(accessorialInfo.CostInvocation))
            {
                paramArray[14].Value = accessorialInfo.CostInvocation;
            }
            else
            {
                paramArray[14].Value = DBNull.Value;
            }
            paramArray[15].Value = accessorialInfo.IsActive;
            paramArray[0x10].Value = accessorialInfo.CreatedBy;
            paramArray[0x11].Value = accessorialInfo.UpdatedBy;
            if (!string.IsNullOrEmpty(accessorialInfo.CostType))
            {
                paramArray[0x12].Value = accessorialInfo.CostType;
            }
            else
            {
                paramArray[0x12].Value = DBNull.Value;
            }
            paramArray[0x13].Value = accessorialInfo.Rate;
            return paramArray;
        }

        public int UpdateAccessorial(AccessorialInfo accessorialInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                accessorialInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_Accessorial_Parameters(accessorialInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_ACCESSORIAL.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int UpdateAccessorial(DataBase dataBase, DbTransaction tran, AccessorialInfo accessorialInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                accessorialInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_Accessorial_Parameters(accessorialInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_ACCESSORIAL.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

